import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store";
import * as routeInfo from "./routers";
import { system } from "./fiexdRouter";
Vue.use(VueRouter);

let routerList = [];
if (process.env.NODE_ENV === "development") {
  //调试页面
  // 
  
}
routerList = routerList.concat(system);

const routes = [
  {
    path: "/",
    name: "index",
    // redirect: to => {
    //   return '/login'
    // },
    component: () => import("@/views/index.vue"),
    meta: {},
    children: [],
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login.vue"),
    meta: {
      noRequireAuth: true,
    },
  },
];

const router = new VueRouter({
  mode: "history", //去掉url中的#
  routes,
});

export function initDynamicRoutes() {
  const currentRoutes = router.options.routes;
  const rightList = store.state.rightList;
  var onRouter;

  rightList.forEach((item) => {
    if (/\//.test(item.route)) {
      const temp = routeInfo[item.route.replace(/\//g, "_")];
      console.log('temptemptemp',temp)
      if (temp) {
        temp.meta.title = item.title;
        currentRoutes[0].children.push(temp);
        if (!onRouter) onRouter = item.route;
      }
    }
    if (Array.isArray(item.sub_menus) && item.sub_menus.length > 0) {
      item.sub_menus.forEach((v) => {
        const temp = routeInfo[v.route.replace(/\//g, "_")];
        if (temp) {
          temp.meta.title = v.title;
          currentRoutes[0].children.push(temp);
          if (!onRouter) onRouter = v.route;
        }
      });
    }
  });
 
  currentRoutes[0].children.push(...routerList);
  router.addRoutes(currentRoutes);
  return onRouter;
}

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

export default router;
